package weaver.interfaces.cese.duss.action.fee;

import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.cese.duss.ceseutil.CostUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-10-23
 * @Description: 费用报销申请流程插入预算执行信息表
 * @ModifyLog:
 **/
/*
 *@Param:
 *@return:
 *@Author: Dusongsong
 *@Date: 2020/10/23
 */
public class WeaCostReimbursementAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String workflowid = requestInfo.getWorkflowid();//获取节点类型
        int nodeid = requestInfo.getRequestManager().getNodeid();
        String currentDate = requestInfo.getRequestManager().getCurrentDate();//当前日期
        String currentNodeType = CostUtil.getCurrentNodeType( workflowid, requestid );//当前节点
        String yslx = "";//状态
        writeLog( "----WeaCostReimbursementAction-------" + requestid );
        try {
            String tablename = getTablename(requestid);
            rs.executeQuery( "select id,yshd,cblrzx,fysjfsrq,ywsqd from "+tablename+" where requestid=?", requestid );
            rs.next();
            String mainid = Util.null2String( rs.getString( "id" ) );//mainid
            String ywsqd = Util.null2String( rs.getString( "ywsqd" ) );//业务申请单
            String yshd = Util.null2String( rs.getString( "yshd" ) );//预算活动
            String cblrzx = Util.null2String( rs.getString( "cblrzx" ) );//成本/利润中心
            String fysjfsrq = Util.null2String( rs.getString( "fysjfsrq" ) );//费用实际发生日期
            writeLog( "-----------currentNodeType" + currentNodeType );
            if (yshd != "") {
                if (!"1".equals( currentNodeType )) {
                    yslx = "0";
                    CostUtil.deleteYszxxxb( requestid );
                    rs.executeQuery( "select fykm,hdje from "+tablename+"_dt1 where mainid=?", mainid );
                    while (rs.next()) {
                        String fykm = Util.null2String( rs.getString( "fykm" ) );//费用科目
                        String hdje = Util.null2String( rs.getString( "hdje" ) );//核对金额
                        //费用报销信息更新到预算执行信息表
                        CostUtil.insertYszxxxb( requestid, yshd, fykm, cblrzx, fysjfsrq, hdje, yslx, fysjfsrq );
                    }
                } else {
                    yslx = "1";
                    CostUtil.deleteYszxxxb( requestid );
                    rs.executeQuery( "select fykm,hdje from "+tablename+"_dt1 where mainid=?", mainid );
                    while (rs.next()) {
                        String fykm = Util.null2String( rs.getString( "fykm" ) );//费用科目
                        String hdje = Util.null2String( rs.getString( "hdje" ) );//核对金额
                        //费用报销信息更新到预算执行信息表
                        CostUtil.insertYszxxxb( requestid, yshd, fykm, cblrzx, fysjfsrq, hdje, yslx, fysjfsrq );
                    }
                }
                if (!"".equals( ywsqd )) {//有事前申请
                    yslx = "2";
                    //费用报销信息更新到预算执行表---释放
                    String dylc = CostUtil.getYwsqd2Requestid( ywsqd );//业务申请单
                    String sql2 = "update uf_yszxxxb set yslx=?,sfrq=? where lc=?";
                    rs.executeUpdate( sql2, new Object[]{yslx, currentDate, dylc} );
                    rs.writeLog( sql2, String.join( ",", yslx, currentDate, dylc ) );
                }
            }
            return Action.SUCCESS;
        } catch (
                Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }
    public static String getTablename(String requestid){
        RecordSet rs = new RecordSet();
        String sql = "select abs(formid) formid from workflow_base where id = (select workflowid from workflow_requestbase where requestid = "+requestid+")";
        rs.executeQuery(sql);
        rs.next();
        String formid = Util.null2String(rs.getString("formid"));
        String tablename = "formtable_main_"+formid;
        return tablename;
    }
}
